﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpHomeEngine;
using DeviceInterfaceTest;
using ControllerTest;
using Insteon;
using Insteon.Devices;

namespace SharpHomeTestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable Table;
        StateEngine Engine;

        private void StartButton_Click(object sender, EventArgs e)
        {

            //for now this is posing as the configurator.
            //the part of the application that loads the settings, and starts the processes.

            Engine = StateEngine.CreateStateEngine();
            Engine.StateChange += new TagStateChangeDelegate(StateEngine_StateChange);
            Engine.StartStateChangeProcessor();

            TestDeviceInterface Interface = new TestDeviceInterface(Engine, "TestInterface");

            TestDimmerSwitch LivingRoomDimmer = new TestDimmerSwitch(Interface, "LivingRoomDimmer");
            TestLightSwitch KitchenSwitch = new TestLightSwitch(Interface, "KitchenSwitch");
            TestDimmerSwitch BedroomDimmer = new TestDimmerSwitch(Interface, "BedroomDimmer");
            TestLightSwitch PorchSwitch = new TestLightSwitch(Interface, "PorchSwitch");
            Interface.RegisterDevice(LivingRoomDimmer);
            Interface.RegisterDevice(KitchenSwitch);
            Interface.RegisterDevice(BedroomDimmer);
            Interface.RegisterDevice(PorchSwitch);

            Interface.Init();
            Interface.Start();

            String CommPort = "";
            Console.WriteLine(String.Format("System.Environment.OSVersion = {0}", System.Environment.OSVersion));
            if (System.Environment.OSVersion.VersionString.Contains("Windows"))
            {
                CommPort = "COM1";
            }
            else if (System.Environment.OSVersion.VersionString.Contains("Unix"))
            {
                CommPort = "/dev/ttyS0";
            }

            InsteonInterface Insteon = new InsteonInterface(Engine, "Insteon1", new InsteonAddress("AA.AA.AA"), CommPort);
            LampLinc LampLinc = new LampLinc(Insteon, "LampLinc", new InsteonAddress("18.80.87") );
            TriggerLinc TriggerLinc = new TriggerLinc(Insteon, "TriggerLinc", new InsteonAddress("14.B4.ED"));
            MotionSensor MotionSensor1 = new MotionSensor(Insteon, "MotionSensor1", new InsteonAddress("14.C0.CC"));
            MotionSensor MotionSensor2 = new MotionSensor(Insteon, "MotionSensor2", new InsteonAddress("14.BF.9F"));
            SwitchLincDimmer SwitchLincDimmer = new SwitchLincDimmer(Insteon, "DeckLight", new InsteonAddress("1C.86.7C"));
            Insteon.RegisterDevice(LampLinc);
            Insteon.RegisterDevice(TriggerLinc);
            Insteon.RegisterDevice(MotionSensor1);
            Insteon.RegisterDevice(MotionSensor2);
            Insteon.RegisterDevice(SwitchLincDimmer);
            Insteon.Init();
            Insteon.Start();


            TestController Controller = new TestController(Engine, "TestController");
            Controller.Init();
            Controller.Start();




        }

        void StateEngine_StateChange(SharpHomeEngine.IChangeSource Source, long Handle, SharpHomeEngine.Tag Tag, object Value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    DisplayMessage(Source, Handle, Tag, Value);
                }));
            }
            else
            {
                DisplayMessage(Source, Handle, Tag, Value);
            }
        }

        Dictionary<String, DataRow> DataRows = new Dictionary<string, DataRow>();
        int counter = 0;

        void DisplayMessage(SharpHomeEngine.IChangeSource Source, long Handle, SharpHomeEngine.Tag Tag, object Value)
        {
            Object StoredValue = Engine.GetValueByAddress(Tag.Address);
            EventBox.Text = String.Format("{0} {1} {2} = {3} ({4})", Source.Name, Handle, Tag.Address, Value, StoredValue);
            if (StoredValue != Value)
            {
                counter++;
            }

            Counter.Text = counter.ToString();

            DataRow Row;
            if (DataRows.ContainsKey(Tag.Address)){
                Row = DataRows[Tag.Address];
            } else {
                Row = Table.NewRow();
                Table.Rows.Add(Row);
                DataRows.Add(Tag.Address, Row);
                StateView.Refresh();
            }
            
            Row["Address"] = Tag.Address;
            Row["Value"] = StoredValue;
            Row["Req Handle"] = Handle;
            Row["Event Source"] = Source.Name;
            Row["Event Value"] = Value;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Table = new DataTable();
            Table.Columns.Add("Address");
            Table.Columns.Add("Value");
            Table.Columns.Add("Req Handle");
            Table.Columns.Add("Event Source");
            Table.Columns.Add("Event Value");
            StateView.DataSource = Table;
            StateView.Columns[0].Width = 200;
        }

        private void Counter_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
